<template>
  <div id="acceptanceList">
    <Filters
      block
      :form="searchForm"
      :filterData="filterData"
      @confirm="changePage(1)"
    />
    <div class="container">
      <DataTable
        rowKey="checkId"
        :tableData="Data"
        :tableColumn="tableColumn"
        :pageConfig="pageConfig"
        @refresh="getList"
      />
    </div>
    <CommonDialog :rows="rows" :form="form" title="作废原因" :visible="formVisible" @hideDialog="hideFormVisible" @confirm="confirmHandle()"/>
  </div>
</template>
<script lang='ts'>
import { Confirm } from '@/decorator';
import { Component, Watch, Vue } from 'vue-property-decorator';

@Component
export default class acceptanceList extends Vue {
  formVisible:any = false
  searchForm: any = {
    isStorage: undefined,
    date: [],
    markTime: [],
    isContract: undefined,
    word: '',
    provinceId: '',
    cityId: '',
    areaId: '',
    materialName: '',
    checkSn: ''
  }
  get filterData () {
    return {
      // isStorage: {
      //   label: '是否生成入库',
      //   type: 'select',
      //   width: 200,
      //   template: {
      //     placeholder: '请选择是否生成入库',
      //     clearable: true,
      //     filterable: true,
      //     options: this.$enums.acceptanceList.typeOptions
      //   }
      // },
      date: {
        label: '验收日期',
        type: 'daterange',
        template: {
          clearable: true
        }
      },
      markTime: {
        label: '编制日期',
        type: 'daterange',
        template: {
          clearable: true
        }
      },
      // isContract: {
      //   label: '是否满足合同要求',
      //   type: 'select',
      //   width: 200,
      //   template: {
      //     placeholder: '请选择是否满足合同要求',
      //     clearable: true,
      //     filterable: true,
      //     options: this.$enums.acceptanceList.typeOptions
      //   }
      // },
      word: {
        label: '项目名称/请购部门/验收人/供应商',
        type: 'input',
        template: {
          placeholder: '请输入项目名称/请购部门/验收人/供应商',
          clearable: true
        }
      },
      materialName: {
        label: '验收物料',
        type: 'input',
        template: {
          placeholder: '请输入验收物料',
          clearable: true
        }
      },
      checkSn: {
        label: '验收编号',
        type: 'input',
        template: {
          placeholder: '请输入验收编号',
          clearable: true
        }
      },
      status: {
        label: '状态',
        type: 'select',
        width: 200,
        template: {
          placeholder: '请选择状态',
          clearable: true,
          filterable: true,
          options: this.$enums.acceptanceList.statusOptions
        }
      }
      // provinceId: {
      //   label: '省',
      //   type: 'select',
      //   template: {
      //     options: this.provinceList,
      //     callback: this.getCity
      //   }
      // },
      // cityId: {
      //   label: '市',
      //   type: 'select',
      //   template: {
      //     options: this.cityList,
      //     callback: this.getArea
      //   }
      // },
      // areaId: {
      //   label: '市',
      //   type: 'select',
      //   template: {
      //     options: this.areaList
      //   }
      // }
    }
  }
  Data: any[] = [];
  tableColumn: any = {
    projectName: {
      label: '项目名称'
    },
    vdef3Name: {
      label: '请购部门'
    },
    checkSn: {
      label: '验收单编号'
    },
    supplierName: {
      label: '供应商'
    },
    checkTime: {
      label: '验收日期'
    },
    texture: {
      label: '验收物料'
    },
    carNum: {
      label: '车牌号'
    },
    // isContract: {
    //   label: '是否满足合同高要求',
    //   map: {
    //     false: '否',
    //     true: '是'
    //   },
    //   width: 100
    // },
    checkPeople: {
      label: '验收人'
    },
    makeTime: {
      label: '编制日期'
    },
    // isStorage: {
    //   label: '是否生成入库',
    //   map: {
    //     false: '否',
    //     true: '是'
    //   },
    //   width: 100
    // },
    checkAcceptStatus: {
      label: '状态',
      map: this.$enums.acceptanceList.status,
      width: 100
    },
    action: {
      label: '操作',
      width: 150,
      template: [
        // {
        //   tag: 'router-link',
        //   text: '编辑',
        //   query: {
        //     _alias: 'edit',
        //     checkId: ''
        //   },
        //   show: ({status}: any) => ['dtj'].includes(status),
        //   path: '/acceptance/acceptanceList/operation'
        // },
        // {
        //   tag: 'router-link',
        //   text: '重新提交',
        //   query: {
        //     _alias: 'edit',
        //     checkId: ''
        //   },
        //   show: ({checkAcceptStatus}: any) => ['no', 'chehui'].includes(checkAcceptStatus),
        //   path: '/acceptance/acceptanceList/operation'
        // },
        // {
        //   tag: 'a',
        //   text: '撤回',
        //   show: ({checkAcceptStatus}: any) => ['dfh'].includes(checkAcceptStatus),
        //   callback: this.handleCancel
        // },
        {
          tag: 'router-link',
          text: '详情',
          query: {
            _alias: 'detail',
            checkId: ''
          },
          show: ({checkAcceptStatus}: any) => ['dsh', 'ok', 'nc', 'no', 'dfy', 'dtj', 'yszf'].includes(checkAcceptStatus),
          path: '/acceptance/acceptanceList/operation'
        },
        {
          text: '审核',
          tag: 'router-link',
          query: {
            _alias: 'aduit',
            checkId: ''
          },
          show: ({checkAcceptStatus, hasAudit}: any) => ['dsh'].includes(checkAcceptStatus) && hasAudit === 'Y',
          path: '/acceptance/acceptanceList/operation'
        }
        // {
        //   tag: 'a',
        //   text: '作废',
        //   show: ({checkAcceptStatus}: any) => !['yszf'].includes(checkAcceptStatus),
        //   callback: this.showFormVisible
        // }
        // {
        //   tag: 'a',
        //   text: '推送NC',
        //   show: ({status}: any) => ['ok'].includes(status),
        //   callback: this.handlePush
        // }
      ]
    }
  };
  pageConfig: any = {
    current: 1,
    total: 10,
    pageSize: 10,
    onChange: this.changePage
  };
  form: any = {
    cancelReason: '',
    checkId: ''
  };
  get rows () {
    return {
      cancelReason: {
        label: '作废原因',
        placeholder: '请输入作废原因',
        type: 'textarea',
        rules: [{ required: true, message: '请输入作废原因', trigger: 'blur' }]
      }

    }
  }

  get getSearchForm () {
    let {date, markTime} = this.searchForm
    let [makeStartTime, makeEndTime] = markTime
    let [checkStartTime, checkEndTime] = date
    return {
      ...this.searchForm,
      markTime: undefined,
      date: undefined,
      makeStartTime,
      makeEndTime,
      checkStartTime,
      checkEndTime
    }
  }

  init () {
    // this.getProvince()
    this.getList();
  }
  getList () {
    let data = {
      ...this.getSearchForm,
      page: this.pageConfig.current,
      size: this.pageConfig.pageSize
    };
    this.$api.checkListFetch(data).then((res: any) => {
      res.data.records.map((item:any) => {
          if (item.isContract == 1) {
            item.isContract = true
          } else {
            item.isContract = false
          }
        })
        this.Data = res.data.records;
        this.pageConfig.total = res.data.total;
        if (!this.Data.length && this.pageConfig.current > 1) {
          --this.pageConfig.current;
          this.getList();
        }
    });
  }
  hideFormVisible () {
    this.formVisible = false
    this.form.cancelReason = ''
    this.form.checkId = ''
  }
  changePage (val: number) {
    this.pageConfig.current = val;
    this.getList();
  }
  @Confirm('', ({ checkSn }: any) => {
    return new Promise((resolve, reject) => {
      resolve({ content: `确定撤回[${checkSn}]验收单吗?`, width: 550 })
    })
  })
  handleCancel ({ checkId: id }: any) {
    this.$api.checkWithdrawPortFetch({ id, checkAcceptStatus: 'chehui' }).then(() => {
      this.$message.success('撤回成功')
      this.getList()
    })
  }
  @Confirm('', ({ checkSn }: any) => {
    return new Promise((resolve, reject) => {
      resolve({ content: `确定把[${checkSn}]的验收单推送NC吗?`, width: 550 })
    })
  })
  handlePush ({ checkId: id }: any) {
    this.$api.checkWithdrawPortFetch({ id, checkAcceptStatus: 'nc' }).then(() => {
      this.$message.success('推送成功')
      this.getList()
    })
  }
  // 显示输入框
  showFormVisible (val:any) {
    console.log(val)
    this.formVisible = true
    this.form.checkId = val.checkId
  }
  // 点击确定作废
  confirmHandle () {
    this.$api.cancelCheckCancelCheckFetch(this.form).then(res => {
      if (res.code == 0) {
        this.$message.success('操作成功')
        this.getList()
        this.hideFormVisible()
      }
    }).catch()
  }
}
</script>
<style lang='scss' scoped>
</style>
